package com.fc.advbox.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fc.advbox.dao.BaseDao;
import com.fc.advbox.dao.OrgMonthProfitDao;
import com.fc.advbox.entity.AdvOrgAcctEntity;
import com.fc.advbox.entity.AdvOrgEntity;
import com.fc.advbox.entity.AdvOrgMonthProfitEntity;
import com.fc.advbox.entity.AdvUserEntity;
import com.fc.advbox.model.org.OrgMonthProfitOverview;
import com.fc.advbox.service.IOrgAcctService;
import com.fc.advbox.service.IOrgMonthProfitService;
import com.fc.advbox.service.IOrgService;
import com.fc.advbox.service.IUserService;
import com.fc.advbox.utils.RRException;
import com.fc.advbox.utils.StringUtils;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Created by E7450 on 2018/7/14.
 */
@Service("orgMonthProfitService")
public class OrgMonthProfitServiceImpl  extends BaseServiceImpl<AdvOrgMonthProfitEntity> implements IOrgMonthProfitService {

    @Autowired
    private OrgMonthProfitDao orgMonthProfitDao;

    @Autowired
    private IOrgAcctService orgAcctService;

    @Autowired
    private IUserService userService;

    @Override
    public BaseDao getDao() {
        return orgMonthProfitDao;
    }

    @Override
    public Map<String, Object> queryListByOrgType(Map<String, Object> params) {
        Long userId = Long.valueOf(String.valueOf(params.get("userId")));
        AdvUserEntity userEntity = userService.queryObject(userId);
        if(userEntity == null)
        {
            throw new RRException("通过userId未找到用户信息, 请核对userId的值!");
        }
        if (userEntity.getOrgId() == null || userEntity.getOrgId() == 0)
        {
            throw new RRException("未找到该用户所属组织!");
        }
        params.put("orgId", userEntity.getOrgId());

        AdvOrgAcctEntity orgAcctEntity = orgAcctService.queryByOrgId(userEntity.getOrgId());

        JSONObject resp = new JSONObject();
        resp.put("orgAcct", orgAcctEntity);
        resp.put("monthProfitList", orgMonthProfitDao.queryList(params));
        return resp;
    }

    @Override
    public List<OrgMonthProfitOverview> queryOrgMonthlyProfitReport(JSONObject params) {
        Map<Long, OrgMonthProfitOverview> overviewMap = Maps.newHashMap();
        List<Long> orderOrgIdList = new ArrayList<>();
        List<AdvOrgMonthProfitEntity> profitEntityList = orgMonthProfitDao.queryOrgMonthlyProfitReport(params);
        if(profitEntityList != null && !profitEntityList.isEmpty())
        {
            for (AdvOrgMonthProfitEntity profitEntity : profitEntityList)
            {
                OrgMonthProfitOverview profitOverview = overviewMap.computeIfAbsent(profitEntity.getOrgId(), k -> new OrgMonthProfitOverview());
                profitOverview.setOrgId(profitEntity.getOrgId());
                if(!StringUtils.isEmpty(profitEntity.getOrgName()))
                {
                    profitOverview.setOrgName(profitEntity.getOrgName());
                }
                profitOverview.setTotalProfit(profitOverview.getTotalProfit().add(profitEntity.getProfit()));
                profitOverview.getMonthProfitList().add(profitEntity);
            }
        }

        List<OrgMonthProfitOverview> respList = new ArrayList<>();
        for(Long orgId : overviewMap.keySet())
        {
            respList.add(overviewMap.get(orgId));
        }

        return respList;
    }
}
